﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortWithTemplate
{
    /// <summary>
    /// Person Class for template
    /// </summary>
    public class PersonTemplate
    {
        #region Constructors

        /// <summary>
        /// Empty constructor
        /// </summary>
        public PersonTemplate()
            : this(0, string.Empty, string.Empty, 0)
        {
        }

        /// <summary>
        /// Full constructor for person
        /// </summary>
        /// <param name="code">Person's code</param>
        /// <param name="firstname">Person's firstname</param>
        /// <param name="lastname">Person's lastname</param>
        /// <param name="bdate">Person's birthday</param>
        public PersonTemplate(int code, string firstName, string lastName, int age)
        {
            this.Code = code;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Comparing persons by Code
        /// </summary>
        /// <param name="person2">Person to compare</param>
        /// <returns>Result of comparation</returns>
        bool CompareCode(PersonTemplate person2)
        {
            bool result = true;
            if (Code > person2.Code)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Comparing persons by Age
        /// </summary>
        /// <param name="person2">Person to compare</param>
        /// <returns>Result of comparation</returns>
        bool CompareAge(PersonTemplate person2)
        {
            bool result = true;
            if (Age > person2.Age)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// Comparing persons by FirstName
        /// </summary>
        /// <param name="person2">Person to compare</param>
        /// <returns>Result of comparation</returns>
        bool CompareFirstName(PersonTemplate person2)
        {
            bool result = true;
            for (int i = 0; i < (FirstName.Length > person2.FirstName.Length ? person2.FirstName.Length : FirstName.Length); i++)
            {
                if (FirstName.ToCharArray()[i] > person2.FirstName.ToCharArray()[i]) return false;
                if (FirstName.ToCharArray()[i] < person2.FirstName.ToCharArray()[i]) return true;
            }
            return result;
        }

        /// <summary>
        /// Comparing persons by LastName
        /// </summary>
        /// <param name="person2">Person to compare</param>
        /// <returns>Result of comparation</returns>
        bool CompareLastName(PersonTemplate person2)
        {
            bool result = true;
            for (int i = 0; i < (LastName.Length > person2.LastName.Length ? person2.LastName.Length : LastName.Length); i++)
            {
                if (LastName.ToCharArray()[i] > person2.LastName.ToCharArray()[i]) return false;
                if (LastName.ToCharArray()[i] < person2.LastName.ToCharArray()[i]) return true;
            }
            return result;
        }

        /// <summary>
        /// Template for compare Persons
        /// </summary>
        /// <param name="person2">Person to compare</param>
        /// <param name="CompareType">Type of comporation</param>
        /// <returns>Result of comparation</returns>
        public bool CompareTemplate(PersonTemplate person2, string compareType)
        {
            switch (compareType)
            {
                case "Code":
                    return CompareCode(person2);
                case "Age":
                    return CompareAge(person2);
                case "FirstName":
                    return CompareFirstName(person2);
                case "LastName":
                    return CompareLastName(person2);
                default:
                    return true;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Person Code
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// Person FirstName
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// Person LastName
        /// </summary>
        public string LastName { get; set; }

        /// <summary>
        /// Person Age
        /// </summary>
        public int Age { get; set; }

        #endregion Properties
    }
}
